package com.example.demo.config;

import com.example.demo.task.service.TaskSchedulerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @Auther Created by l.wang on 2019/1/18.
 */
@Configuration
@EnableScheduling
public class TaskConfigure implements SchedulingConfigurer {

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setPoolSize(20);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }

    @Bean
    public TaskSchedulerService taskService(){
        TaskSchedulerService taskSchedulerService = new TaskSchedulerService();
        taskSchedulerService.setScheduler(threadPoolTaskScheduler());
        return taskSchedulerService;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar registrar)
    {
        TaskScheduler scheduler = this.threadPoolTaskScheduler();
        registrar.setTaskScheduler(scheduler);
    }
}
